package com.wxg.druid.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSourceFactory;

public class DruidUtils {

	/**
	 * 根据提供的"config"加载properties配置文件
	 * 
	 * @param config
	 * @return
	 * @throws IOException
	 */
	public static Properties loadProp(String config){
		Properties prop = new Properties();
		InputStream is = DruidUtils.class.getClassLoader().getResourceAsStream(config);
		try {
			try {
				prop.load(is);
			} finally {
				is.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return prop;
	}
	
	/**
	 * 获取"DataSource"
	 * @param config
	 * @return
	 */
	public static DataSource fetchDS(String config){
		DataSource ds = null;
		Properties prop = loadProp(config);
		try {
			ds = DruidDataSourceFactory.createDataSource(prop);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ds;
	}
	
	
	
	
}
